﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace DocumentsTagger.UI
{

    public partial class PdfViewer : UserControl, IViewer
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string path,
                                                  [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
                                                  int shortPathLength);

        public event ControlLoadedHandler ControlLoaded;

        public PdfViewer()
        {
            InitializeComponent();
            this.axAcroPDF1.EndInit();
        }

        /// <summary>
        /// displays the document file.
        /// </summary>
        /// <remarks>We convert files to short name (8.3) to enable opening of unicode names files</remarks>
        /// <param name="FileName">file name</param>
        public void LoadData(string fileName)
        {
            
            Thread t = new Thread(new ParameterizedThreadStart(bg_DoWork));
            t.Name = "PdfViewerThread";
            t.SetApartmentState(ApartmentState.STA);
            t.Start(fileName);
             
            //bg_DoWork((object)fileName);
            
        }

        /// <summary>
        /// will be run when the loading thread is done getting the data from the file itself.
        /// </summary>
        void bg_RunWorkerCompleted()
        {
            if (this.InvokeRequired)
            {
                Action act = () => bg_RunWorkerCompleted();
                this.Invoke(act, null);
            }
            else
            {
                onControlLoaded();
            }
        }

        /// <summary>
        /// this is the event trigger.
        /// </summary>
        private void onControlLoaded()
        {
            if (ControlLoaded != null)
            {
                ControlLoaded();
            }
        }

        /// <summary>
        /// this is function that loads the preview. 
        /// happens in a diffrent thread from the UI!
        /// </summary>
        /// <param name="param"></param>
        void bg_DoWork(object param)
        {
            string fileName = (string)param;
            StringBuilder shortPath = new StringBuilder(255);
            GetShortPathName(fileName, shortPath, shortPath.Capacity);
            
            // this.axAcroPDF1.LoadFile(shortPath.ToString());
            Action act = () => this.axAcroPDF1.LoadFile(shortPath.ToString()); 
            this.Invoke(act, null);

            bg_RunWorkerCompleted();
        }

        private void PdfViewer_Load(object sender, EventArgs e)
        {

        }
        
    }
}
